﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class StringEx
    {
        /// <summary>
        /// Adds a suffix to a given path
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public static String AddSuffixToFilePath(this string filePath, string suffix)
        {
            int lastDotIndex = filePath.LastIndexOf('.');
            if (lastDotIndex > 0)
            {
                string ext = filePath.Substring(lastDotIndex);
                string pathWithoutExtension = filePath.Remove(lastDotIndex);
                return String.Format("{0}{1}{2}", pathWithoutExtension, suffix, ext);
            }
            else
            {
                return filePath;
            }
        }

        public static String GetSuffix(this string filePath)
        {
            string result = String.Empty;
            int suffixBeginIndex = filePath.LastIndexOf('_');
            int suffixEndIndex = filePath.LastIndexOf('.');
            if (suffixBeginIndex > 0 && suffixEndIndex > 0)
            {
                try
                {
                    result = filePath.Substring(suffixBeginIndex, suffixEndIndex - suffixBeginIndex);
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
    }
}
